#!/bin/sh

cat >/etc/sysctl.d/99-tuning.conf <<-EOF
	net.ipv4.tcp_fastopen = 3
EOF

mem="$(awk '/^MemTotal:/{print $2}' /proc/meminfo)"
if [ "$mem" -gt 65536 ]; then
	rmem=$((mem * 48))
	wmem=$((mem * 48))

	cat >>/etc/sysctl.d/99-tuning.conf <<-EOF
		net.core.rmem_max = $rmem
		net.core.rmem_default = $rmem
		net.core.wmem_max = $wmem
		net.core.wmem_default = $wmem
		net.core.netdev_max_backlog = 2048
		net.ipv4.tcp_adv_win_scale = 4
	EOF
fi

/etc/init.d/sysctl restart

touch '/etc/crontabs/root'
/etc/init.d/cron enable
/etc/init.d/cron restart

rm -f /usr/lib/lua/luci/view/admin_status/index/mwan.htm
rm -f /usr/lib/lua/luci/view/admin_status/index/upnp.htm
rm -f /usr/lib/lua/luci/view/admin_status/index/ddns.htm
rm -f /usr/lib/lua/luci/view/admin_status/index/minidlna.htm

sed -i 's/\"services\"/\"nas\"/g' /usr/lib/lua/luci/controller/aria2.lua
sed -i 's/services/nas/g' /usr/lib/lua/luci/view/aria2/overview_status.htm
sed -i 's/\"services\"/\"nas\"/g' /usr/lib/lua/luci/controller/hd_idle.lua
sed -i 's/\"services\"/\"nas\"/g' /usr/lib/lua/luci/controller/samba.lua
sed -i 's/\"services\"/\"nas\"/g' /usr/lib/lua/luci/controller/minidlna.lua
sed -i 's/\"services\"/\"nas\"/g' /usr/lib/lua/luci/controller/transmission.lua
sed -i 's/\"services\"/\"nas\"/g' /usr/lib/lua/luci/controller/mjpg-streamer.lua
sed -i 's/\"services\"/\"nas\"/g' /usr/lib/lua/luci/controller/p910nd.lua
sed -i 's/\"services\"/\"nas\"/g' /usr/lib/lua/luci/controller/usb_printer.lua

uci -q batch <<-EOF >/dev/null
	set luci.main.mediaurlbase=/luci-static/material
	commit luci
EOF

sed -i '/log-facility=/d' /etc/dnsmasq.conf
echo "log-facility=/dev/null" >>/etc/dnsmasq.conf

# sed -i '/REDIRECT\s\s*--to-ports\s\s*53\s*$/d' /etc/firewall.user
# {
# 	echo ""
# 	echo "iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 53"
# 	echo "iptables -t nat -A PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 53"
# } >>/etc/firewall.user
# sed -i '/^\s*$/d' /etc/firewall.user
# echo "" >>/etc/firewall.user

#####################防止LAN与WAN网段重合#########################
lan_ip=$(uci -q get network.lan.ipaddr)

if [ -n "$lan_ip" ]; then
	wan_proto="$(uci -q get network.wan.proto)"
	need_detect=1
	if [ "$wan_proto" = "dhcp" ]; then
		/etc/init.d/network restart
		for i in $(seq 1 15); do
			sleep 1
			wan_up=$(ifstatus wan | jsonfilter -e '@["up"]')
			[ x"$wan_up" = x"true" ] || continue
			wan_ip=$(ifstatus wan | jsonfilter -e '@["ipv4-address"][0].address')
			[ -n "$wan_ip" ] && break
		done
	elif [ "$wan_proto" = "static" ]; then
		wan_ip="$(uci -q get network.wan.ipaddr)"
	else
		need_detect=0
	fi

	if [ "$need_detect" -eq 1 ] && [ -n "$wan_ip" ]; then
		wan_ip1=$(echo "$wan_ip" | cut -d '.' -f1)
		wan_ip2=$(echo "$wan_ip" | cut -d '.' -f2)
		wan_ip3=$(echo "$wan_ip" | cut -d '.' -f3)

		lan_ip1=$(echo "$lan_ip" | cut -d '.' -f1)
		lan_ip2=$(echo "$lan_ip" | cut -d '.' -f2)
		lan_ip3=$(echo "$lan_ip" | cut -d '.' -f3)
		lan_ip4=$(echo "$lan_ip" | cut -d '.' -f4)

		if [ "$wan_ip1.$wan_ip2.$wan_ip3" = "$lan_ip1.$lan_ip2.$lan_ip3" ]; then
			lan_ip3=$(((lan_ip3 + 1) % 255))
			lan_ip="$lan_ip1.$lan_ip2.$lan_ip3.$lan_ip4"
			uci -q set network.lan.ipaddr="$lan_ip"
			# 后面有uci commit network，所以此处不commit，减少不必要的network服务重启
		fi
	fi
fi
##################### 防止LAN与WAN网段重合 结束 #########################

ula_prefix="2d$(dd if=/dev/urandom bs=1 count=1 2>/dev/null | hexdump -e '1/1 "%02x"'):$(dd if=/dev/urandom bs=2 count=1 2>/dev/null | hexdump -e '2/1 "%02x"'):$(dd if=/dev/urandom bs=2 count=1 2>/dev/null | hexdump -e '2/1 "%02x"')::/48"

uci -q batch <<-EOF >/dev/null
	set dhcp.lan.ra='server'
	set dhcp.lan.dhcpv6='server'
	set dhcp.lan.ra_management='1'
	set dhcp.lan.ra_default='1'
	set dhcp.odhcpd.loglevel='3'
	commit dhcp
	set network.globals.ula_prefix="$ula_prefix"
	set network.wan.mtu='1492'
	set network.lan.stp='1'
	commit network
EOF

rm -rf /tmp/luci-modulecache/
rm -f /tmp/luci-indexcache

/etc/init.d/network restart

exit 0
